package com.ktmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.ktmall.pojo.*;
import com.ktmall.service.CartItemService;
import com.ktmall.utils.RespMsg;
import com.ktmall.vo.CartItemVo;
import com.ktmall.vo.PreOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "用户购物车相关接口")
@RestController
@RequestMapping("/uni/member/cart")
public class CartItemController {
    @Autowired
    private CartItemService cartItemService;

    @ApiOperation("添加商品购物车")
    @PostMapping("/addCart")
    public RespMsg addCart(@RequestBody CartItem cart) {
        int i = cartItemService.insert(cart);
        if (i > 0) {
            return RespMsg.success("添加成功", cart);
        } else {
            return RespMsg.fail("添加失败");
        }
    }

    @ApiOperation("根据登录用户查询购物车列表")
    @PostMapping("/findCartGoodsList")
    public RespMsg findCartGoodsList(@RequestBody Integer userId) {
        List<CartItem> cartItemList = cartItemService.findByUserId(userId);
        List<CartItemVo> cartItemVos = new ArrayList<>();
        //将List<cartItem> list集合的数据转换成List<CartItemVo> vo集合的数据，再响应
        if (cartItemList != null && cartItemList.size() > 0) {
            for (CartItem c : cartItemList) {
                SkuItem skuItem = c.getSkuItem();
                GoodsItem goodsItem = skuItem.getGoods();
                String attrsText = "";
                for (AttrItem spec : skuItem.getSpecs()) {
                    for (AttrValues value : spec.getValues()) {
                        attrsText += value.getName() + " ";
                    }
                }
                CartItemVo vo = new CartItemVo(c.getId(), goodsItem.getId(), skuItem.getId(), goodsItem.getGoodsName(), skuItem.getPicture(), c.getGoodsCount(), skuItem.getPrice(), Integer.valueOf(skuItem.getInventory() + ""), c.getSelected() == 1, attrsText);
                vo.setNowPrice(new BigDecimal("0.9").multiply(vo.getPrice()).setScale(2));
                cartItemVos.add(vo);
            }
        }
        return RespMsg.success("用户购物车列表获取成功", cartItemVos);
    }

    @ApiOperation("批量删除用户的购物车列表")
    @PostMapping("/delCart")
    public RespMsg delCart(@RequestBody Integer [] ids) {
        int i = cartItemService.batchDel(ids);
        if (i > 0) {
            return RespMsg.success("删除成功!", ids);
        } else {
            return RespMsg.fail("删除失败!");
        }
    }

    @ApiOperation("更新用户购物车商品数量")
    @GetMapping("/updateCount")
    public RespMsg updateCount(Integer id, Integer goodsCount) {
        int i = cartItemService.update(id, goodsCount);
        if (i > 0) {
            return RespMsg.success("修改成功！");
        } else {
            return RespMsg.fail("修改失败！");
        }
    }

    @ApiOperation("修改用户购物车列表商品选中")
    @GetMapping("/updateSelected")
    public RespMsg updateSelected(Integer id, Boolean selected) {
        int i = cartItemService.updateSelected(id, selected  ? 1 : 0);
        if (i > 0) {
            return RespMsg.success("修改成功！");
        } else {
            return RespMsg.fail("修改失败！");
        }
    }

    @ApiOperation("修改用户购物车列表商品全选或全不选")
    @GetMapping("/updateAllSelect")
    public RespMsg updateAllSelect(Integer userId, Boolean selected) {
        int i = cartItemService.updateAllSelected(userId, selected ? 1 : 0);
        if (i > 0) {
            return RespMsg.success("修改成功！");
        } else {
            return RespMsg.success("修改失败！");
        }
    }

    @ApiOperation("")
    @GetMapping("/preOrder")
    public RespMsg<PreOrder> getPreOrder(Integer userId){
        PreOrder preOrder = cartItemService.getPreOrder(userId);
        return RespMsg.success("预付订单加载成功",preOrder);
    }
}
